package com.taotao.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
/**
 * 购物车controller
 * @author zqx the monster
 *
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.service.CartServie;

@Controller
@RequestMapping("/cart")
public class CartController {
	@Autowired
	private CartServie cartServie;

	@RequestMapping("/add/{itemId}")
	public String addCartItem(@PathVariable long itemId, @RequestParam(defaultValue = "1") Integer num,
			HttpServletRequest request, HttpServletResponse response) {
		cartServie.addCartItem(itemId, num, request, response);
		return "redirect:/cart/success.html";
	}

	@RequestMapping("/success")
	public String showSuccess() {
		return "cartSuccess";
	}

	// 展示购物车列表
	@RequestMapping("/cart")
	public String getCartItemList(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CartItem> list = cartServie.getCartItemList(request, response);
		model.addAttribute("cartList", list);
		return "cart";
	}

	@RequestMapping("/update/num/{itemId}")
	public String updateItemNum(@PathVariable long itemId, Integer num, HttpServletRequest request,
			HttpServletResponse response) {
		cartServie.updateNum(itemId, num, request, response);
		return "redirect:/cart/success.html";
	}

	@RequestMapping("/delete/{itemId}")
	public String deleteItem(@PathVariable long itemId, HttpServletRequest request, HttpServletResponse response) {
		cartServie.deleteItem(itemId, request, response);
		return "redirect:/cart/cart.html";
	}

}
